﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Z13qu.Model;
using Z13qu.Code;

namespace Z13qu.Data
{
    public class DbContextInitializer : DropCreateDatabaseIfModelChanges<KoalaDbContext>
    {
        //CreateDatabaseIfNotExists<KoalaDbContext>
        protected override void Seed(KoalaDbContext context)
        {
            Sys_Role role = new Sys_Role();
            role.Id = Common.GuId();
            role.EnCode = "administrators";
            role.RoleName = "超级管理员";
            role.CreatorTime = DateTime.Now;
            role.AllowEdit = true;
            role.AllowDelete = true;
            role.SortCode = 1;
            role.IsDelete = false;
            role.IsEnabled = true;
            context.Sys_Role.Add(role);

            Sys_User user = new Sys_User();
            user.Id = Common.GuId();
            user.RoleId = role.Id;
            user.Account = "admin";
            user.CreatorTime = DateTime.Now;
            user.RealName = "超级管理员";
            user.NickName = user.RealName;
            user.IsDelete = false;
            user.IsEnabled = true;
            user.IsAdministrator = true;
            context.Sys_User.Add(user);

            Sys_UserLogon userLogon = new Sys_UserLogon();
            userLogon.Id = Common.GuId();
            userLogon.UserId = user.Id;
            userLogon.FirstVisitTime = DateTime.Now;
            userLogon.UserSecretkey = "z13qu.com";
            userLogon.Password = Md5Helper.Create(DESEncrypt.Encrypt(Md5Helper.Create("z13qu").ToLower(), userLogon.UserSecretkey).ToLower()).ToLower(); ;
            context.Sys_UserLogon.Add(userLogon);

            #region Sys_Module
            Sys_Module module = new Sys_Module();
            module.Id = Common.GuId();
            module.ParentId = "0";
            module.Name = "系统管理";
            module.Icon = "fa fa-gears";
            module.IsMenu = false;
            module.IsExpand = true;
            module.IsDelete = false;
            module.IsEnable = true;
            module.AllowDelete = false;
            module.AllowEdit = false;
            module.SortCode = 2;
            module.CreatorTime = DateTime.Now;
            context.Sys_Module.Add(module);

            Sys_Module module2 = new Sys_Module();
            module2.Id = Common.GuId();
            module2.ParentId = module.Id;
            module2.Name = "系统菜单";
            module2.UrlAddress = "/SystemManage/Module/Index";
            module2.IsMenu = true;
            module2.IsExpand = false;
            module2.IsDelete = false;
            module2.IsEnable = true;
            module2.AllowDelete = false;
            module2.AllowEdit = false;
            module2.SortCode = 7;
            module2.CreatorTime = DateTime.Now;
            context.Sys_Module.Add(module2);
            #endregion

            #region Sys_RoleAuthorize

            Sys_RoleAuthorize roleAuthorize = new Sys_RoleAuthorize();
            roleAuthorize.Id = Common.GuId();
            roleAuthorize.CreatorTime = DateTime.Now;
            roleAuthorize.ItemId = module.Id;
            roleAuthorize.ItemType = ItemType.模块;
            roleAuthorize.ObjectId = role.Id;
            roleAuthorize.ObjectType = ObjectType.角色;
            roleAuthorize.CreatorTime = DateTime.Now;
            context.Sys_RoleAuthorize.Add(roleAuthorize);

            Sys_RoleAuthorize roleAuthorize2 = new Sys_RoleAuthorize();
            roleAuthorize2.Id = Common.GuId();
            roleAuthorize2.CreatorTime = DateTime.Now;
            roleAuthorize2.ItemId = module2.Id;
            roleAuthorize2.ItemType = ItemType.模块;
            roleAuthorize2.ObjectId = role.Id;
            roleAuthorize2.ObjectType = ObjectType.角色;
            roleAuthorize2.CreatorTime = DateTime.Now;
            context.Sys_RoleAuthorize.Add(roleAuthorize2); 
            #endregion


            base.Seed(context);
        }
    }
}
